package com.carleasoft.mps.adu.business.login.controller;

import com.carleasoft.mps.adu.business.login.bean.*;
import com.carleasoft.mps.adu.business.login.service.LoginService;
import com.carleasoft.mps.core.perm.AllCache;
import com.carleasoft.mps.core.result.OkResult;
import com.carleasoft.mps.core.result.R;
import com.carleasoft.mps.security.permission.constants.SecurityConsts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * <p>Description: 登录 controller</p>
 *
 * @author ztm
 * @since 2020/4/28
 */
@Api(value = "login", tags = "用户登录")
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;


    @ApiOperation(value = "执行登录", notes = "执行登录")
    @PostMapping("/toLogin")
    public R toLogin(HttpServletResponse response, @RequestBody ToLoginVo toLoginVo) {

        LoginResultVo loginResultVo = loginService.toLogin(toLoginVo);

        response.setHeader(SecurityConsts.RESPONSE_TOKEN_HEADER, loginResultVo.getToken());
        response.setHeader("Access-Control-Expose-Headers", SecurityConsts.RESPONSE_TOKEN_HEADER);

        return R.ok(loginResultVo);
    }

    @PostMapping("/buildUser/{userAccount}")
    public R<AllCache> buildUser(@PathVariable(name="userAccount") String userAccount) {
        return R.ok(loginService.buildUser(userAccount));
    }

    /**
     * 获取登录用户菜单权限资源
     * @param loginUserMenuPermPo 請求參數
     * @return
     */
    @PostMapping("/getLoginUserMenuPerm")
    public R<LoginUserPermVo> getLoginUserMenuPerm(@RequestBody LoginUserMenuPermPo loginUserMenuPermPo) {
        return R.ok(loginService.getLoginUserMenuPerm(loginUserMenuPermPo.getUserAccount(), loginUserMenuPermPo.getLoginType()));
    }


    @ApiOperation(value = "退出登录", tags = "退出登录")
    @PostMapping("/bowOut")
    public R bowOut(@RequestBody BowOutVo bowOutVo) {

        loginService.bowOut(bowOutVo);
        return R.result(OkResult.SUCCESS);
    }

}
